//
//  OAAddressBookCatalogViewController.m
//  ERP
//
//  Created by goldeney on 15/1/21.
//  Copyright (c) 2015年 ShangHai Goldeneye Techonology Co.,Ltd. All rights reserved.
//

#import "OAAddressBookCatalogViewController.h"
#import "OAContactPersonListViewController.h"

#import "MJRefresh.h"
#import "OAAddressBookCatalogViewCell.h"
#import "UITableView+Custom.h"
#import "ServiceConnect.h"

#import "NSKeyedArchiverUserLogin.h"
#import "NSDictionary+DataFormat.h"

@interface OAAddressBookCatalogViewController ()

@property (nonatomic, strong) NSArray *addressBookArray;

@end

@implementation OAAddressBookCatalogViewController
@synthesize tableView = _tableView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        [self setTitle: @"通讯录"];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.addressBookArray = [NSArray array];
    
    self.tableView.tableFooterView= [[UIView alloc] init];
    [self setupRefresh];
}

- (void)getNetWorkDataToAddressBook
{
    //机构数类型(1:公文机构职位树,2:消息管理机构职位树,3:通讯录关联公司列表树)
    UserLoginNSCoding *userInfo = [NSKeyedArchiverUserLogin getUserLoginData];
    ServiceParametModel *model = [[ServiceParametModel alloc] init];
    [model setValue:userInfo.Id forKey:@"userid"];
    [model setValue:@3 forKey:@"type"];
    
    ServiceConnect *content = [[ServiceConnect alloc] initWithMethodName:@"OA_GetTree_Public" dataArray:model];
    [content startConnectNetworkCompleted:^(ServiceResultModel *result, NSDictionary *userInfo) {
        [self stopRefresh];//关闭下拉菜单
        //添加数据，刷新页面
        if(result.resultArray && result.resultArray.count>0){
            self.addressBookArray = result.resultArray;
            [self.tableView reloadData];
        } else {
            [self.tableView setTableViewNotDataDrawText:@"暂无通讯目录" isShow:YES];
        }
    } failed:^(NSError *error, NSDictionary *userInfo) {
        NSLog(@"==failed===%@",error);
        [self errorInfoViewText:@"哦，出错了"];
        
    } abnormal:^(NSError *error, NSDictionary *userInfo) {
        NSLog(@"==abnormal===%@",error);
        [self errorInfoViewText:@"网络异常"];
    }];
}

- (void)errorInfoViewText:(NSString*)label
{
    [self.tableView headerEndRefreshing];
    [self.tableView footerEndRefreshing];
    
    if (self.tableView.visibleCells.count <= 0) {
        [self.tableView setTableViewNotDataDrawText:label isShow:YES];
    } else {
        [self.tableView setTableViewNotDataDrawText:nil isShow:NO];
        [self showErrorHUDText:label];
    }
}

- (void)setupRefresh
{
    //添加下拉加载数据
    [self.tableView addHeaderWithTarget:self action:@selector(headerRereshing)];
    [self.tableView headerBeginRefreshing];
}

- (void)stopRefresh
{
    //移除下拉加载数据
    [self.tableView headerEndRefreshing];
    [self.tableView footerEndRefreshing];
}

#pragma mark 开始进入刷新状态
- (void)headerRereshing
{
    [self getNetWorkDataToAddressBook];
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.addressBookArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifierOne = @"OAAddressBookCatalogViewCell";
    OAAddressBookCatalogViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierOne];
    if (!cell)
        cell = [[OAAddressBookCatalogViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifierOne];
    
    NSDictionary *dictionary = [self.addressBookArray objectAtIndex:indexPath.row];
    cell.titleLable.text = [dictionary objectFormatStringForKey:@"orgName"];
    cell.compLable.text = [dictionary objectFormatStringForKey:@"parentName"];
    
    return cell;
}

#pragma mark
#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 64;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSDictionary *dictionary = [self.addressBookArray objectAtIndex:indexPath.row];
    
    OAContactPersonListViewController *viewController = [[OAContactPersonListViewController alloc] initWithValue:dictionary];
    [self.navigationController pushViewController:viewController animated:YES];
}

@end

